package com.changfang.gold_man.service.Imp;

import com.changfang.gold_man.common.ImageUtil;
import com.changfang.gold_man.common.MD5Utils;
import com.changfang.gold_man.common.SendSmsUtil;
import com.changfang.gold_man.entity.Goods;
import com.changfang.gold_man.entity.Img;
import com.changfang.gold_man.entity.Secret;
import com.changfang.gold_man.entity.User;
import com.changfang.gold_man.mapper.GoodsMapper;
import com.changfang.gold_man.mapper.UserMapper;
import com.changfang.gold_man.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.xml.bind.DatatypeConverter;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.changfang.gold_man.common.SendSmsUtil.sendSms;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private CarServiceImpl carServiceImpl;
    //查找所有用户
    @Override
    public List<User> findAll() {
        List<User> userList = userMapper.findAll();
        return userList;

    }

    //通过ID查找用户信息
    @Override
    public User findId(String user_id) {
        User user = userMapper.findId(user_id);
        return user;
    }

    //删除用户
    @Override
    public void deleteUser(String user_id) {
        userMapper.deleteUser(user_id);
    }

    //添加用户
    @Override
    public String addUser(Map<String, String> user) {
        User user1 = new User();
        user1.setUser_id(user.get("user_id"));
        user1.setUser_pass(user.get("user_pass"));
        user1.setUser_name(user.get("user_name"));
        user1.setUser_age(user.get("user_age"));
        user1.setUser_gold(Float.parseFloat(user.get("user_gold")));
        user1.setUser_score(user.get("user_score"));
        user1.setUser_img(user.get("user_img"));
        user1.setUser_sex(user.get("user_sex"));
        user1.setUser_bg(user.get("user_bg"));
        //判断数据库是否存在相同id
        if (userMapper.findId(user.get("user_id")) != null) {
            System.out.println("用户已存在");
            return "用户已存在,注册失败";
        } else {
            userMapper.addUser(user1);
            return "1";
        }
    }
    //重置密码
    @Override
    public String updatePass(String user_id, String user_pass) {
        try {
            userMapper.updatePass(user_id,user_pass);
            return "SUCCESS";
        }catch (Exception e){
            return "ERROR";
        }
    }

    //修改用户信息
    @Override
    public String updateUser(Map<String, String> user) {
        try {
            String user_id = user.get("user_id");
            String user_name = user.get("user_name");
            String user_age = user.get("user_age");
            String user_sex = user.get("user_sex");
            String user_pass = user.get("user_pass");
            String user_img = user.get("user_img");
            String user_bg = user.get("user_bg");
            //MD5加密
//            String passMD5 = MD5Utils.encrypt(user_pass, MD5Utils.MD5_KEY);
//            System.out.println(passMD5);
            if (user_pass.equals("")){
                userMapper.updateUserNP(user_id,user_name,user_sex,user_age,user_img,user_bg);
            }else {
                userMapper.updateUser(user_id,user_name,user_sex,user_age,user_pass);
            }
            return "修改信息成功";
        }catch (Exception e){
            return "修改信息失败,"+e;
        }

    }


    //用户注册
    @Override
    public String register(Map<String, String> user) {
        User user1 = new User();
        String u1id = user.get("user_id");
        String u1pass = user.get("user_pass");
        String u1name = user.get("user_id");
        user1.setUser_score("0");
        user1.setUser_id(u1id);

        //MD5加密
//        user1.setUser_pass(MD5Utils.encrypt(u1pass, MD5Utils.MD5_KEY));
        //普通密码
        user1.setUser_pass(u1pass);

        user1.setUser_name(u1name);
            //判断数据库是否存在相同id
            if (userMapper.findId(u1id) != null) {
                return "用户已存在,注册失败";
            } else {
                userMapper.addUser(user1);
                return "1";
            }

    }

    //验证用户密码
    @Override
    public Boolean checkPass(String user_id, String user_pass) {
        if (userMapper.findId(user_id) != null) {
            String dataPass = userMapper.findId(user_id).getUser_pass();
//            String newPass = MD5Utils.encrypt(user_pass, MD5Utils.MD5_KEY);
            String newPass = user_pass;
            if (dataPass.equals(newPass)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    //用户登录
    @Override
    public User login(Map<String,String> user) {
        String user_id = user.get("user_id");
        String user_pass = user.get("user_pass");
        if (checkPass(user_id, user_pass)) {
            User user1 = findId(user_id);
            return user1;
        } else {
            return null;
        }
    }
//发送短信验证码
    @Override
    public String getCode(String user_id,String templateId) {
//        创建发送短信类实例
        SendSmsUtil sendSmsUtil = new SendSmsUtil();
//        创建发送号码组
        String[] Phones = {"+86"+user_id};
//        调用发送短信方法
        //注册1336598重置1371851
        String code = sendSms(Phones,templateId);
//        返回验证码到前端
        return code;
    }

    //获取校园密语
    @Override
    public List<Secret> getSecret() {
        return  userMapper.getSecret();
    }
    //添加校园密语
    @Override
    public String addSecret(String user_id,String secret_msg){
        try {
            userMapper.addSecret(user_id,secret_msg);
            return "success";
        }catch (Exception e){
            return "error";
        }
    }
    //删除校园密语
    @Override
    public String deleteSecret(String secret_id){
        try {
            userMapper.deleteSecret(Integer.parseInt(secret_id));
            return "1";
        }catch (Exception e){
            return "0";
        }
    }
    //金币更新
    @Override
    public void updateGold(String user_id, String user_gold) {
        Float gold = findId(user_id).getUser_gold();
        if (gold==null){
            gold= Float.parseFloat("0");
        }
        //判定运算符号'a'增加，‘r’减少
        String sign =  user_gold.substring(0,1);
        //判断数值
        String value = user_gold.substring(1);
        if (sign.equals("a")){
            Float newGold = gold+Float.parseFloat(value);
            System.out.println(newGold);
            userMapper.updateGold(user_id,newGold);

        }else if (sign.equals("r")){
            float newGold = gold-Float.parseFloat(value);
            System.out.println(newGold);
            userMapper.updateGold(user_id,newGold);

        }
    }
    //积分更新
    @Override
    public void updateScore(String user_id, String user_score) {
        System.out.println(user_id);
        System.out.println(findId(user_id));
        String scoreStr = !ObjectUtils.isEmpty(findId(user_id).getUser_score()) ? findId(user_id).getUser_score():"0";
        Float score = Float.parseFloat(scoreStr);

        //符号
        String sign =  user_score.substring(0,1);
        //数值
        String value = user_score.substring(1);
        if (sign.equals("a")){
            String newScore = Float.toString(score+Float.parseFloat(value));
            userMapper.updateScore(user_id,newScore);
        }
        if (sign.equals("r")){
            String newScore = Float.toString(score-Float.parseFloat(value));
            userMapper.updateScore(user_id,newScore);
        }

    }
    //购买商品
    @Override
    public void buyGoods(Map<String, String> buyInfo) {
        System.out.println("进入购买");
        String user_id =  buyInfo.get("user_id");
        String goods_id = buyInfo.get("goods_id");
        Goods goods = goodsMapper.findGoodsId(Integer.parseInt(goods_id));
        User user = userMapper.findId(user_id);
        Float goods_gold = goods.getGoods_gold();
        System.out.println("开始交易");
        //买家付钱，加积分
        System.out.println("r"+Float.toString(goods_gold));
        updateGold(user_id,"r"+Float.toString(goods_gold));
        updateScore(user_id,"a5");
        //卖家收钱，加积分
        String seller_id = goods.getUser_id();
        updateGold(seller_id,"a"+Float.toString(goods_gold));
        updateScore(seller_id,"a5");
        //更改商品拥有者为买家
        goodsMapper.updateUserId(Integer.parseInt(goods_id),user_id);
        //商品下架
        goodsMapper.goodsDeal(Integer.parseInt(goods_id),"0");
        System.out.println("退出购买");
    }
    //租用车辆
    @Override
    public void rentCar(Map<String, String> carInfo) {
        String car_id = carInfo.get("car_id");
        //借用者id
        String person_id = carInfo.get("person_id");
        String user_id = carInfo.get("user_id");
        String car_gold = carInfo.get("car_gold");
        Map<String,String> carStatu = new HashMap();
        carStatu.put("car_statu","0");
        carStatu.put("car_id",car_id);
        carServiceImpl.updateStatu(carStatu);
        carServiceImpl.updatePerson(car_id,person_id);
        updateGold(user_id,"a"+car_gold);
        updateGold(person_id,"r"+car_gold);
        updateScore(user_id,"a5");
        updateScore(person_id,"a5");
    }
    //点赞
    @Override
    public String like(String secret_id,Integer like) {
        try {
            userMapper.like(Integer.parseInt(secret_id),like);
            return "OK";
        }catch (Exception e){
            return  "ERROR";
        }

    }


    //修改用户权限
    @Override
    public String changePms(String user_id,Integer user_permission) {
        try {
            userMapper.changePms(user_id,user_permission);
            return "修改权限成功";
        }catch (Exception e){
            return "出错了！修改权限失败";
        }


    }

}
